/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_httpd.htm
 *
 * Zhiqim Httpd is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.httpd.entities;

import java.io.File;
import java.io.IOException;

import org.zhiqim.httpd.HttpContext;
import org.zhiqim.httpd.HttpEntity;
import org.zhiqim.httpd.HttpHeader;
import org.zhiqim.httpd.HttpSender;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.kernel.util.Streams;

/**
 * 跨域处理器
 *
 * @version v1.0.0 @author zouzhigang 2014-3-21 新建与整理
 */
public class CrossdomainEntity implements HttpEntity
{
    private byte[] crossdomainBytes;
    private long crossdomainModified;
    
    public CrossdomainEntity(HttpContext context)
    {
        String crossdomainPath = context.getResourcePath() + _PATH_CROSSDOMAIN_;
        if (context.isClasspath())
        {//类路径的，直接缓存，不存在的直接使用默认的跨域文件
            crossdomainBytes = Streams.getBytesClassPathExceptionNull(CrossdomainEntity.class, crossdomainPath);
            if (crossdomainBytes == null){
                crossdomainBytes = Streams.getBytesClassPathExceptionNull(CrossdomainEntity.class, _PATH_CROSSDOMAIN_);
            }
            crossdomainModified = System.currentTimeMillis();
        }
        else
        {//文件目录，如果存在缓存，不存在直接使用默认的跨域文件
            File file = new File(crossdomainPath);
            crossdomainBytes = Streams.getBytesFilePathExceptionNull(file);
            if (crossdomainBytes != null){
                crossdomainModified = file.lastModified();
            }else{
                crossdomainBytes = Streams.getBytesClassPathExceptionNull(CrossdomainEntity.class, _PATH_CROSSDOMAIN_);
                crossdomainModified = System.currentTimeMillis();
            }
        }
    }
    
    @Override
    public boolean isMatch(String pathInContext)
    {
        return _PATH_CROSSDOMAIN_.equals(pathInContext);
    }
    
    @Override
    public void handle(HttpHeader header, HttpSender sender) throws IOException
    {
        HttpContext context = header.getContext();
        if (!context.isClasspath())
        {//文件目录，需要检查是否被替换
            String crossdomainPath = context.getResourcePath() + _PATH_CROSSDOMAIN_;
            File file = new File(crossdomainPath);
            if (file.exists() && file.lastModified() != crossdomainModified)
            {
                byte[] bytes = Streams.getBytesFilePathExceptionNull(file);
                if (bytes != null)
                {
                    crossdomainBytes = bytes;
                    crossdomainModified = file.lastModified();
                }
            }
        }
        
        String ifModifiedSince = header.getHeader(_IF_MODIFIED_SINCE_);
        String crossdomainModifiedHttp = DateTimes.getDateTimeHttp(crossdomainModified);
        if (ifModifiedSince != null && ifModifiedSince.indexOf(crossdomainModifiedHttp) != -1)
        {//时间相等
            sender.setHeader(_PRAGMA_, _NO_CACHE_);
            sender.setHeader(_CACHE_CONTROL_, _NO_CACHE_);
            sender.sendError(_304_NOT_MODIFIED_);
            return;
        }

        sender.setContentType(_TEXT_CDP_);
        sender.setHeader(_LAST_MODIFIED_, crossdomainModifiedHttp);
        sender.setHeader(_PRAGMA_, _NO_CACHE_);
        sender.setHeader(_CACHE_CONTROL_, _NO_CACHE_);
        if (header.isMethodResponseContent()){
            sender.write(crossdomainBytes);
        }
        sender.commit();
    }
}
